import 'dart:html' as html show window;

import 'package:file_picker/file_picker.dart';

import '../../platform_service.dart';
import '../platform_service_platform_interface.dart';

///
/// ProjectName: platform_service
/// Author: lizhi
/// CreateDate: 2022/12/28 10:19
/// Copyright: ©2022 NEW CORE Technology Co. Ltd. All rights reserved.
/// Description: 通用web 选文件、选图片
///
class BaseWebPlatform implements PlatformServicePlatform {
  @override
  Future<String?> getPlatformVersion() {
    final version = html.window.navigator.userAgent;
    return Future.value(version);
  }

  @override
  Future<NDFilePickerResult> pickFile() async {
    try {
      FilePickerResult? result =
          await FilePicker.platform.pickFiles(allowMultiple: true);
      if (result?.files.length == 1) {
        await Future.delayed(const Duration(milliseconds: 700), () {});
      }
      return Future.value(NDFilePickerResult(result?.files ?? []));
    } catch (err) {
      print('err：${err.toString()}');
      return Future.value(NDFilePickerResult([]));
    }
  }

  @override
  Future<List<NDFileInfo>> pickImages(
      {int maxCount = 8,
      bool enableCamera = false,
      bool isWaterMark = false,
      bool isWaterMarkTiled = false,
      List<ImageWaterMarkSettingModel>? waterMarkSettings}) async {
    try {
      List<NDFileInfo> selectImages = [];
      FilePickerResult? result = await FilePicker.platform
          .pickFiles(allowMultiple: true, type: FileType.image);

      if (result != null) {
        if (result.files.length == 1) {
          await Future.delayed(const Duration(milliseconds: 700), () {});
        }
        for (var file in result.files) {
          selectImages.add(NDFileInfo(
            name: file.name,
            size: file.size,
            fileBytes: file.bytes?.toList() ?? <int>[],
          ));
        }
      }
      return Future.value(selectImages);
    } catch (err) {
      print('err：${err.toString()}');
      return Future.value([]);
    }
  }

  @override
  Future<NDFileInfo?> takePicture(
      {bool isWaterMark = false,
      bool isWaterMarkTiled = false,
      List<ImageWaterMarkSettingModel>? waterMarkSettings}) async {
    throw UnimplementedError('unSupport takePicture()!');
  }
}
